package jhsys.mc.device;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import jhsys.mc.smarthome.data.DeviceConfig;
import jhsys.mc.smarthome.data.DeviceUtil;



public class AJLB {
	private String name;
	private String ico;
	private int type;
	private int id;
	private short state;
	private String file;

	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getId() {
		return id;
	}


	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIco() {
		return ico;
	}
	public void setIco(String ico) {
		this.ico = ico;
	}
	public short getState() {
		return state;
	}
	public void setState(short state) {
		this.state = state;
	}
	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
	public static List<AJLB> read(){
		List<AJLB> result=new ArrayList<AJLB>();
		String filename=DeviceConfig.filePath+DeviceConfig.AJLB_filename;
		RandomAccessFile randomA = null;
		try {
			randomA = new RandomAccessFile(filename, "rw");
			byte[] lclb=new byte[72];
			while(randomA.read(lclb)==72){
				AJLB temp=new AJLB();
				temp.setName(DeviceUtil.byteToString(lclb, 0, 20));
				temp.setIco(DeviceUtil.byteToString(lclb, 20, 20));
				temp.setType(DeviceUtil.byteTounsigned(lclb[40]));
				temp.setId(DeviceUtil.byte4ToInt(lclb, 44));
				temp.setState(DeviceUtil.byte2ToShort(lclb, 48));
				temp.setFile(DeviceUtil.byteToString(lclb, 52, 20));
				result.add(temp);
				lclb=new byte[72];
			}
			randomA.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
		
	}
	
	
	public static void write(List<AJLB> list){
		RandomAccessFile randomA = null;
		try {
			File file = new File(DeviceConfig.filePath
					+ DeviceConfig.FJLB_filename);
			if (file.exists()) {
				file.delete();
				file.createNewFile();
			}
			randomA = new RandomAccessFile(file, "rw");
			byte[] fjlb;
			for (AJLB item : list) {
				fjlb = new byte[72];
				System.arraycopy(DeviceUtil.stringTobyte(item.getName()), 0, fjlb, 0, 20);
				System.arraycopy(DeviceUtil.stringTobyte(item.getIco()), 0, fjlb, 20, 20);
				System.arraycopy(DeviceUtil.unsignedbyteTobyte(item.getType()), 0, fjlb, 40, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getId()), 0, fjlb, 44, 4);
				System.arraycopy(DeviceUtil.shortTobytes(item.getState()), 0, fjlb, 48, 4);
				System.arraycopy(DeviceUtil.stringTobyte(item.getFile()), 0, fjlb, 52, 20);
				randomA.write(fjlb);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	public String toString() {
		return "AJLB [file=" + file + ", ico=" + ico + ", id=" + Integer.toHexString(id) + ", name="
				+ name + ", state=" + Integer.toHexString(state) + ", type=" + Integer.toHexString(type) + "]";
	}


}
